import 'package:flutter/material.dart';
import 'package:flutter_app/utils/log_util.dart';
import 'package:flutter_app/utils/web_load_state.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';

///author: create by SunJH
///时间: 2021/12/24 0024
///邮箱: 965989158@qq.com
///描述: web_flutter测试页
///

class WebFlutter extends StatelessWidget {
  WebFlutter({Key? key}) : super(key: key);
  var loadState = WebLoadState.unLoad.obs;
  var isLoadSuccessFull = false;

  InAppWebViewController? webController;
  final InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
      ),
      android: AndroidInAppWebViewOptions(
          useHybridComposition: true,
          supportMultipleWindows: true,
          domStorageEnabled: true),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

  @override
  Widget build(BuildContext context) {
    var inAppWebView = InAppWebView(
      initialOptions: options,
      onWebViewCreated: (webViewController) {
        webController = webViewController;
      },
      initialUrlRequest:URLRequest(url: Uri.parse("https://appg7voenr64739.h5.xiaoeknow.com/")),
      androidOnPermissionRequest: (controller, origin, resources) async {
        return PermissionRequestResponse(
            resources: resources,
            action: PermissionRequestResponseAction.GRANT);
      },
      onLoadStart: (controller, url) {
        LogUtil.logI("页面开始加载:地址是${url.toString()}");
        isLoadSuccessFull = true;
        loadState.value = WebLoadState.loading;
      },
      onLoadStop: (controller, url) {
        LogUtil.logI("页面停止加载:地址是${url.toString()}");
        //完成页面加载
        loadState.value =
        isLoadSuccessFull ? WebLoadState.success : loadState.value;
      },
      onLoadError: (controller, url, code, message) {
        LogUtil.logI("onLoadError");
        loadState.value = WebLoadState.loadFail;

      },
      onLoadHttpError: (controller, url, code, message) {
        LogUtil.logI("onLoadHttpError");
        loadState.value = WebLoadState.loadFail;

      },
      onProgressChanged: (onctroller,pro){
      },
    );
    return Scaffold(
      body: SafeArea(child: Obx(() {
        return Stack(
          children: [
            inAppWebView,
            loadState.value == WebLoadState.loadFail
                ? InkWell(
              onTap: () {
                webController?.reload();
              },
              child: Container(
                color: Colors.white,
                alignment: AlignmentDirectional.center,
                child: const Text("加载失败，点击重新加载"),
              ),
            )
                : const SizedBox(),
            loadState.value == WebLoadState.loading
                ? Container(
              color: Colors.white,
              alignment: AlignmentDirectional.center,
              child: const CircularProgressIndicator(),
            )
                : const SizedBox(),
          ],
        );
      })),
    );
  }
}
